package cn.wjx.taobaounion.ui.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.wjx.taobaounion.R;
import cn.wjx.taobaounion.model.domain.SelectedContent;
import cn.wjx.taobaounion.utils.URLUtils;

/**
 * @author WuChangJian
 * @date 2020/6/26 8:35
 */
public class SelectedPageRightAdapter extends RecyclerView.Adapter<SelectedPageRightAdapter.InnerHolder> {

    List<SelectedContent.DataBean.TbkUatmFavoritesItemGetResponseBean.ResultsBean.UatmTbkItemBean> tbkItems
            = new ArrayList<>();
    private ItemContentClickListener mListener = null;

    @NonNull
    @Override
    public InnerHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_selected_right, parent, false);
        return new InnerHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull InnerHolder holder, int position) {
        SelectedContent.DataBean.TbkUatmFavoritesItemGetResponseBean.ResultsBean.UatmTbkItemBean itemBean = tbkItems.get(position);
        holder.setData(itemBean);
        holder.itemView.setOnClickListener(v-> {
            if (mListener != null) {
                mListener.onItemContentClick(tbkItems.get(position));
            }
        });
    }

    @Override
    public int getItemCount() {
        return tbkItems.size();
    }

    public void setData(SelectedContent content) {
        if (content.getCode() == 10000) {
            List<SelectedContent.DataBean.TbkUatmFavoritesItemGetResponseBean.ResultsBean.UatmTbkItemBean> tbkItem = content.getData().getTbk_uatm_favorites_item_get_response().getResults().getUatm_tbk_item();
            tbkItems.clear();
            tbkItems.addAll(tbkItem);
            notifyDataSetChanged();
        }
    }

    public void setOnItemContentClickListener(ItemContentClickListener listener) {
        this.mListener = listener;
    }
    public interface ItemContentClickListener {
        void onItemContentClick(SelectedContent.DataBean.TbkUatmFavoritesItemGetResponseBean.ResultsBean.UatmTbkItemBean tbkItem);
    }
    public class InnerHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.selected_cover)
        public ImageView mSelectedCover;

        @BindView(R.id.selected_off_price)
        public TextView mSelectedOffPrice;

        @BindView(R.id.selected_buy_btn)
        public TextView mSelectedBuyBtn;

        @BindView(R.id.selected_original_price)
        public TextView mSelectedOriginalPrice;

        @BindView(R.id.selected_title)
        public TextView mSelectedGoodsTitle;

        public InnerHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        public void setData(SelectedContent.DataBean.TbkUatmFavoritesItemGetResponseBean.ResultsBean.UatmTbkItemBean itemBean) {
            Context context = itemView.getContext();
            mSelectedGoodsTitle.setText(itemBean.getTitle());
            if (TextUtils.isEmpty(itemBean.getCoupon_click_url())) {
                mSelectedOriginalPrice.setText("晚啦，没有优惠券了");
                mSelectedBuyBtn.setVisibility(View.GONE);
            } else {
                mSelectedBuyBtn.setVisibility(View.VISIBLE);
                mSelectedOriginalPrice.setText(String.format(context.getString(R.string.selected_goods_original_price), itemBean.getZk_final_price()));
            }
            if (TextUtils.isEmpty(itemBean.getCoupon_info())) {
                mSelectedOffPrice.setVisibility(View.GONE);
            } else {
                mSelectedOffPrice.setVisibility(View.VISIBLE);
                mSelectedOffPrice.setText(itemBean.getCoupon_info());
            }
            if (itemBean.getPict_url() != null) {
                String path = URLUtils.getTicketPath(itemBean.getPict_url());
                Glide.with(context).load(path).into(mSelectedCover);
            }
        }
    }
}
